<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GetFukaPrizeList, PatchPrizeSort, PatchPrizeStatus } from '@/api/modules/fukaact'
import PageNumber from '@/store/modules/PageNumberCache'

const imgDomain = 'https://img1.eprhan.cc'
const route = useRoute()
const router = useRouter()
const PageNumberCache = PageNumber()

// 查询表单
const findFrom = reactive({
  fukaActId: route.query.fukaActId,
  Flag: '~',
  PrizeType: 998,
  PrizeKindid: '',
})

const dataList: any = reactive({
  list: [],
})

const loading = ref(true)
const total = ref(0)
let PageSize = 20

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.fukaActId && { fukaActId: findFrom.fukaActId }),
    ...(findFrom.PrizeType !== 998 && { PrizeType: findFrom.PrizeType }),
    ...(findFrom.PrizeKindid && { PrizeKindid: findFrom.PrizeKindid }),
    ...(findFrom.Flag !== '~' && { Flag: findFrom.Flag }),
  }
  await GetFukaPrizeList(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
    else {
      ElMessage.error(`查询失败。${dataRes.errors}`)
      loading.value = false
    }
  })
    .catch(() => {
      loading.value = false
    })
  loading.value = false
}

// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 修改状态
function ModifyType(params: any) {
  PatchPrizeStatus(params).then((res: any) => {
    if (res.statusCode === 200) {
      ElMessage.success('修改成功')
      // GetDataList(PageNum, PageSize)
    }
    else {
      ElMessage.error(`修改失败${res.errors}`)
    }
  })
}

// 修改排序
function SortModification(message: any) {
  const params = {
    id: message.row.fukaPrizeId,
    sort: message.row.sort,
  }
  PatchPrizeSort(params).then((res) => {
    if (res.data) {
      ElMessage.success('修改成功')
    }
    else {
      ElMessage.error('修改失败')
      GetDataList()
    }
  })
}

// 添加
function AddData() {
  router.push({
    name: 'fukaactPrizeDetail',
    query: {
      fukaActId: findFrom.fukaActId,
    },
  })
}
// 编辑
function EditData(id: number) {
  router.push({
    name: 'fukaactPrizeDetail',
    query: {
      id,
      fukaActId: findFrom.fukaActId,
    },
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
  PageNumberCache.SetFukaPrizePage(val)
}

const nowPageNum = computed(() => {
  return PageNumberCache.GetFukaPrizePage()
})

onMounted(async () => {
  GetDataList(PageNumberCache.GetFukaPrizePage())
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="奖品类型">
                <el-select v-model="findFrom.PrizeType" placeholder="请选择状态">
                  <el-option label="全部" :value="998" />
                  <el-option label="赠品券" :value="1" />
                  <el-option label="实物" :value="2" />
                </el-select>
              </el-form-item>
              <el-form-item :label="`${findFrom.PrizeType === 1 ? '赠品券' : findFrom.PrizeType === 2 ? '商品' : '奖品'}Id`">
                <el-input v-model="findFrom.PrizeKindid" placeholder="请输入奖品ID" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="findFrom.Flag" placeholder="请选择状态">
                  <el-option label="全部" value="~" />
                  <el-option label="正常" :value="1" />
                  <el-option label="停用" :value="2" />
                </el-select>
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="fukaPrizeId" label="ID" width="100" />
        <el-table-column align="center" prop="fukaActId" label="福卡活动ID" width="100" />
        <el-table-column header-align="center" prop="prizeName" label="奖品名称" width="230" />
        <el-table-column align="center" prop="prizePic" label="奖品图" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.prizePic" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="prizeCoverpic" label="奖品封面" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.prizeCoverpic" />
          </template>
        </el-table-column>
        <el-table-column align="center" width="130" prop="prizeType" label="奖品类型">
          <template #default="scope">
            <el-tag>
              {{ scope.row.prizeType === 1 ? '赠品券' : scope.row.prizeType === 2 ? '实物' : scope.row.prizeType === 3 ? '满减券' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="奖品ID" width="100">
          <template #default="scope">
            {{ scope.row.prizeType === 1 ? scope.row.couponInfo.id : scope.row.productInfo.productId }}
          </template>
        </el-table-column>
        <el-table-column header-align="center" label="奖品信息" width="260">
          <template #default="scope">
            <!-- {{ scope.row.prizeType === 1 ? scope.row.couponInfo.id : scope.row.productInfo.productId }} -->
            <div v-if="scope.row.prizeType === 1" class="user-name">
              <div>
                <p style="margin: 0;">
                  {{ scope.row.couponInfo?.name }}
                </p>
                <span>
                  券类型：
                  <el-tag>
                    {{ scope.row.couponInfo?.type === 1 ? '赠品券' : scope.row.couponInfo?.type === 2 ? '奖品券' : scope.row.couponInfo?.type === 3 ? '满减券' : '未定义' }}
                  </el-tag>
                </span>
              </div>
            </div>
            <div v-else class="user-name">
              <el-avatar shape="square" :size="60" style="margin-right: 10px;" :src="imgDomain + scope.row.productInfo?.cdnProductPhotoPic" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.productInfo?.productName }}
                </p>
                <span>
                  {{ scope.row.productInfo?.salePrice }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column header-align="center" width="300" prop="productUrl" label="跳转地址">
          <template #default="scope">
            {{ scope.row.prizeType === 1 ? scope.row.usedUrl : '无' }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="prizeListBg" label="卡券背景图" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.prizeListBg" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="change" label="概率" width="100" />
        <el-table-column align="center" prop="stock" label="库存" width="100" />
        <el-table-column align="center" prop="used" label="已消耗" width="100" />
        <el-table-column header-align="center" prop="desp" label="描述" width="300" />
        <el-table-column align="center" width="240" prop="addTime" label="添加时间" />
        <el-table-column align="center" width="240" prop="updateTime" label="更新时间" />
        <el-table-column align="center" prop="sort" label="排序" fixed="right" width="160">
          <template #default="scope">
            <el-input v-model="scope.row.sort" style="width: 120px;">
              <template #append>
                <el-button type="primary" size="small" @click="SortModification(scope)">
                  修改
                </el-button>
              </template>
            </el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" fixed="right" width="180">
          <template #default="scope">
            <el-switch
              v-model="scope.row.flag"
              active-text="启用"
              inactive-text="停用"
              :active-value="1"
              :inactive-value="2"
              @click="ModifyType({ id: scope.row.fukaPrizeId, enable: scope.row.flag === 1 ? true : false })"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" min-width="150">
          <template #default="scope">
            <el-button type="primary" @click="EditData(scope.row.fukaPrizeId)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 60px;
    margin-right: 10px;
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;

  div {
    text-align: left;
  }
}
</style>
